﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Super.Common;
using Super.Entities;
using Super.Infrastructure;

namespace Super.DomainService
{

    public class SiteStatistics
    {
        /// <summary>
        /// 店铺数量
        /// </summary>
        public int ShopCount { get; set; }

        /// <summary>
        /// 用户数量
        /// </summary>
        public int UserCount { get; set; }

        /// <summary>
        /// 主题数量 店铺+用户
        /// </summary>
        public int AllTopicCount { get; set; }

        /// <summary>
        /// 跟帖数量 店铺+用户
        /// </summary>
        public int AllPostCount { get; set; }

        /// <summary>
        /// 用户主题数量
        /// </summary>
        public int UserTopicCount { get; set; }

        /// <summary>
        /// 用户跟帖数量
        /// </summary>
        public int UserPostCount { get; set; }



    }

    public class StatisticsService
    {
        private readonly DbContextAdapter _adp;
        private readonly BaseService<KeyValue> _keyvalueService;

       public StatisticsService(DbContextAdapter adapter)
        {
            _adp = adapter;
            _keyvalueService = new BaseService<KeyValue>(_adp);
        }

       private void Initi()
       {
           var bankModel = new SiteStatistics();
           var values = JsonHelper.ObjectToJson(bankModel);
           var statisticsModel = _keyvalueService.Repository.Find(a => a.Key == "Statistics").FirstOrDefault();
           if (statisticsModel == null)
           {
               var keyValue=new KeyValue();
               keyValue.Key = "Statistics";
               keyValue.Value = values;
               _keyvalueService.Repository.Insert(keyValue);
               _adp.SaveChanges();
           }
           else
           {
               statisticsModel.Value = values;
               _keyvalueService.Repository.Update(statisticsModel);
               _adp.SaveChanges();
           }
       
       }


        /// <summary>
        /// 获取更新的统计实体
        /// </summary>
        /// <returns></returns>
        private SiteStatistics GetStatisticsModel()
        {
            //Initi();
            var statisticsModel = _keyvalueService.Repository.Find(a => a.Key == "Statistics").FirstOrDefault();
            if (statisticsModel == null)
            {
                return null;
            }
            var siteStatistics = Common.JsonHelper.JsonToObject<SiteStatistics>(statisticsModel.Value);
            return siteStatistics;
        }

        /// <summary>
        /// 更新统计实体
        /// </summary>
        /// <param name="siteStatistics"></param>
        private void UpdateStatisticsModel(SiteStatistics siteStatistics)
        {
            var statisticsKeyValueModel = _keyvalueService.Repository.Find(a => a.Key == "Statistics").FirstOrDefault();
            if (statisticsKeyValueModel == null)
            {
                return;
            }
            var values = Common.JsonHelper.ObjectToJson(siteStatistics);
            statisticsKeyValueModel.Value = values;
            _keyvalueService.Repository.Update(statisticsKeyValueModel);

        }

        /// <summary>
        /// 添加用户数量
        /// </summary>
        public void AddUserCount()
        {
            var statisticsModel = GetStatisticsModel();
            statisticsModel.UserCount += 1;
            UpdateStatisticsModel(statisticsModel);
        }

        /// <summary>
        /// 添加店铺数量
        /// </summary>
        public void AddShopCount()
        {
            var statisticsModel = GetStatisticsModel();
            statisticsModel.ShopCount += 1;
            UpdateStatisticsModel(statisticsModel);
        }

        /// <summary>
        /// 添加所有主题数量
        /// </summary>
        public void AddAllTopicCount()
        {
            var statisticsModel = GetStatisticsModel();
            statisticsModel.AllTopicCount += 1;
            UpdateStatisticsModel(statisticsModel);
        }

        /// <summary>
        /// 添加用户主题数量
        /// </summary>
        public void AddUserTopicCount()
        {
            var statisticsModel = GetStatisticsModel();
            statisticsModel.UserTopicCount += 1;
            statisticsModel.AllTopicCount += 1;
            UpdateStatisticsModel(statisticsModel);
        }

        /// <summary>
        /// 添加所有post数量
        /// </summary>
        public void AddAllPostCount()
        {
            var statisticsModel = GetStatisticsModel();
            statisticsModel.AllPostCount += 1;
            UpdateStatisticsModel(statisticsModel);
        }

        /// <summary>
        /// 添加所有用户post数量
        /// </summary>
        public void AddUserPostCount()
        {
            var statisticsModel = GetStatisticsModel();
            statisticsModel.UserPostCount += 1;
            statisticsModel.AllPostCount += 1;
            UpdateStatisticsModel(statisticsModel);
        }

    }
}
